clear
version 15.0
set matsize 150


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// 																		 /// 
/// 																		 /// 			   	
/// Author			: Kenneth Bunker										 ///	
/// Email			: kabunker@gmail.com									 ///														
/// Project			: Why do parties cooperate in presidentialism?			 ///
///																			 ///
///																			 ///
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


** set working directory:														<------------- SET WORKING DIRECTORY HERE!
cd "/Users/KennethBunker/Dropbox/Bunker - Coalition Formation (REP)/data/dataverse"


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// 																		 
///	PART 1: ELECTORAL COALITIONS
///																			 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


// load data:
use "Coal_Electoral.dta", clear

// only for author*
// keep countryname country ctyalpha year president country_id government_id coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh sqmdmh enep maj outsider age cpichg_lag1 gdpgr_lag1 gini_lag1

// XT set
xtset country_id year 

// Table 2 Model 1
// model
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh sqmdmh outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, re vce(oim) nolog
// check odds ratio
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh sqmdmh outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog

// Table 2 Model 2
// model
xtlogit coal_elect1 enep maj outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, re vce(oim) nolog
// check odds ratio
xtlogit coal_elect1 enep maj outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog

// Table 2 Model 3
// model
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh sqmdmh enep maj outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, re vce(oim) nolog
// check odds ratio
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh sqmdmh enep maj outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// 																		 
///	PART 2: GOVERNMENT COALITIONS
///																			 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


// load data:
use "Coal_Government.dta", clear

// only for author*
// keep countryname country ctyalpha year president country_id government_id coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh sqmdmh enep maj outsider age cpichg_lag1 gdpgr_lag1 gini_lag1

// XT set
xtset country_id year 

// Table 3 Model 4
// model
xtlogit coalgov leg1 nonleg percent1 coal_elect1 crisis left outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, re vce(oim) nolog
// check odds ratio
xtlogit coalgov leg1 nonleg percent1 coal_elect1 crisis left outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog

// Table 3 Model 5
// model
xtlogit coalgov smp reelected term_length legelec_2 unicam totalseats mdmh sqmdmh coal_elect1 crisis left outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, re vce(oim) nolog
// check odds ratio
xtlogit coalgov smp reelected term_length legelec_2 unicam totalseats mdmh sqmdmh coal_elect1 crisis left outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog

// Table 3 Model 6
// model
xtlogit coalgov enlp maj coal_elect1 crisis left outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, re vce(oim) nolog
// check odds ratio
xtlogit coalgov enlp maj coal_elect1 crisis left outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog

// Table 3 Model 7
// model
xtlogit coalgov leg1 nonleg percent1 smp reelected term_length legelec_2 unicam totalseats mdmh sqmdmh enlp maj coal_elect1 crisis left outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, re vce(oim) nolog
// check odds ratio
xtlogit coalgov leg1 nonleg percent1 smp reelected term_length legelec_2 unicam totalseats mdmh sqmdmh enlp maj coal_elect1 crisis left outsider age cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// 																		 
///	HAUSMAN TESTS (EXAMPLE WITH GOVERNMENT COALITIONS)
///																			 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


// test with final model w/ fixed effects
xtlogit coalgov leg1 nonleg percent1 smp reelected term_length legelec_2 unicam totalseats enlp maj coal_elect1 crisis left outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or fe vce(oim) nolog

// store fixed
estimates store fixed

// test with final model w/ random effects
xtlogit coalgov leg1 nonleg percent1 smp reelected term_length legelec_2 unicam totalseats enlp maj coal_elect1 crisis left outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog

// store random
estimates store random

// compare
hausman fixed random


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// 																		 
///	POST-ESTIMATION
///																			 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


// For dummys: probability of electoral coalition higher?

// load data:
use "Coal_Electoral.dta", clear

// Cut continious variables into 4 groups
egen ctotalseats = cut(totalseats), group(4) label
egen cmdmh 	= cut(mdmh), group(4) label
egen cenep 	= cut(enep), group(4) label
egen cmaj 	= cut(maj), group(4) label
egen cage 	= cut(age), group(4) label
egen cpop 	= cut(pop), group(4) label
egen ccpi 	= cut(cpichg_lag1), group(4) label
egen cgdp 	= cut(gdpgr_lag1), group(4) label
egen cgini 	= cut(gini_lag1), group(4) label

// Use marginsplot to graph

// smp
xtlogit coal_elect1 i.smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins smp, predict(pu0)

// reelection_eff2
xtlogit coal_elect1 smp i.reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins reelection_eff2, predict(pu0)

// term_length
xtlogit coal_elect1 smp reelection_eff2 i.term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins term_length, predict(pu0)
 
// legelec_2
xtlogit coal_elect1 smp reelection_eff2 term_length i.legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins legelec_2, predict(pu0)
 
// unicam
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 i.unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins unicam, predict(pu0)
 
// housesys2
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam i.housesys2 thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins housesys2, predict(pu0)
 
// thresh_2
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 i.thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins thresh_2, predict(pu0)
 
// totalseats
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 i.ctotalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins ctotalseats, predict(pu0)
 
// mdmh
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats i.cmdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins cmdmh, predict(pu0)
 
// enep
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh i.cenep maj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins cenep, predict(pu0)
 
// maj
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep i.cmaj outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins cmaj, predict(pu0)
 
// outsider
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj i.outsider age pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins outsider, predict(pu0)
 
// age
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider i.cage pop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins cage, predict(pu0)
 
// pop
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age i.cpop cpichg_lag1 gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins cpop, predict(pu0)
 
// cpichg_lag1
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age pop i.ccpi gdpgr_lag1 gini_lag1 if year<2010, or re vce(oim) nolog
margins ccpi, predict(pu0)
 
// gdpgr_lag1
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 i.cgdp gini_lag1 if year<2010, or re vce(oim) nolog
margins cgdp, predict(pu0)
 
// gini_lag1
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh enep maj outsider age pop cpichg_lag1 gdpgr_lag1 i.cgini if year<2010, or re vce(oim) nolog
margins cgini, predict(pu0)


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// 																		 
///	MARGINS WITH INTERACTIONS
///																			 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


// SMP and ENEP

##FULL REGRESSION
xtlogit coal_elect1 i.smp##c.enep reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or re vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins smp, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// HOUSESYS and ENEP

##FULL REGRESSION
xtlogit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam i.housesys2##c.enep thresh_2 totalseats mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or re vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins housesys2, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// SMP and ENEP

##FULL REGRESSION
logit coal_elect1 i.smp##c.enep reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins smp, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// HOUSESYS and ENEP

##FULL REGRESSION
logit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam i.housesys2##c.enep thresh_2 totalseats mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins housesys2, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// UNICAM and ENEP

##FULL REGRESSION
logit coal_elect1 smp reelection_eff2 term_length legelec_2 i.unicam##c.enep housesys2 thresh_2 totalseats mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins unicam, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// TERM_LENGTH and ENEP

##FULL REGRESSION
logit coal_elect1 smp reelection_eff2 i.term_length##c.enep legelec_2 unicam housesys2 thresh_2 totalseats mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins term_length, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// REELECTION_EFF2

##FULL REGRESSION
logit coal_elect1 smp i.reelection_eff2##c.enep term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins reelection_eff2, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// THRESH_2

##FULL REGRESSION
logit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 i.thresh_2##c.enep totalseats mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins thresh_2, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// TOTALSEATS

##FULL REGRESSION
logit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 i.ctotalseats##c.enep mdmh maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins ctotalseats, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// MDMH

##FULL REGRESSION
logit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats i.cmdmh##c.enep maj outsider age pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins cmdmh, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci

// AGE

##FULL REGRESSION
logit coal_elect1 smp reelection_eff2 term_length legelec_2 unicam housesys2 thresh_2 totalseats mdmh maj outsider i.cage##c.enep pop cpichg_lag1 gdpgr_lag1 cgini if year<2010, or vce(oim) nolog
##PREDICT MARGINS OF SMP, ACCORDING TO ENEP (between 1 and 15, increasing by 1)
margins cage, at(enep=(1 (1) 15)) vsquish
##PLOT MARGINS
marginsplot, noci


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// 																		 
///	THE END!
///																			 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

